package com.yskj.mwk.controller;

import com.yskj.mwk.bean.Address;
import com.yskj.mwk.bean.TempImage;
import com.yskj.mwk.bean.User;
import com.yskj.mwk.service.UserService;
import com.yskj.mwk.utils.JsonResult;
import com.yskj.mwk.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("userApi")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     *获取用户openid
     * */
    @PostMapping("getXcxOpenid")
    public JsonResult getXcxOpenid(@RequestParam String code){
        try {
            String openid = userService.getWxOpenId(code);
            return ResultUtils.success("获取成功",openid);
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 一键登录
     * 获取手机号，根据手机号判断账号是否注册
     */
    @PostMapping("bindTellByOpenid")
    public JsonResult bindTellByOpenid(@RequestParam String code, @RequestParam String openid){
        try {
            User user= userService.getUserTellByCode(code,openid);
            if(user==null){
                return ResultUtils.failure(210,"登录失败");
            }else{
                return ResultUtils.success("信息存在",user);
            }
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 根据openid判断是否登录
     * */
    @PostMapping("checkOpenidIsBind")
    public JsonResult checkOpenidIsBind(@RequestParam String openid){
        try {
            String uuid=userService.getUserUuid(openid);
            if(uuid==null){
                return ResultUtils.failure(210,"未绑定手机号");
            }else{
                return ResultUtils.success("已经绑定",uuid);
            }
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 根据openid判断是否登录
     * */
    @PostMapping("getUserDesc")
    public JsonResult getUserDesc(@RequestParam String uuid){
        try {
            User user=userService.getUserDescByUuid(uuid);
            return ResultUtils.success("获取成功",user);
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 根据uuid修改头像或昵称
     * */
    @PostMapping("updateUserInfo")
    public JsonResult updateUserInfo(@RequestParam String uuid,@RequestParam String headUrl,@RequestParam String name){
        try {
            int result=userService.updateUserInfo(uuid,headUrl,name);
            if(result==1){
                return ResultUtils.success("修改成功",null);
            }else{
                return ResultUtils.failure(210,"修改失败");
            }
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 新增或修改地址
     * */
    @PostMapping("updateAddress")
    public JsonResult updateAddress(
            @RequestParam("id") int id,
            @RequestParam("lxrname") String lxrname,
            @RequestParam("lxrtell") String lxrtell,
            @RequestParam("lxraddress") String lxraddress,
            @RequestParam("province") String province,
            @RequestParam("city") String city,
            @RequestParam("district") String district,
            @RequestParam("isDefault") int isDefault,
            @RequestParam("uuid") String uuid
            ){
        try {
            Address address=new Address();
            address.setId(id);
            address.setLxrname(lxrname);
            address.setLxrtell(lxrtell);
            address.setLxraddress(lxraddress);
            address.setProvince(province);
            address.setCity(city);
            address.setDistrict(district);
            address.setIsDefault(isDefault);
            address.setUuid(uuid);
            if (isDefault==1){
                userService.updateNoAddressDefault(uuid);
            }
            int result=0;
            if (id==0){
                //新增
                result=userService.addAddress(address);
            }else{
                //修改
                result=userService.updateAddress(address);
            }
            if(result==1){
                return ResultUtils.success("操作成功",null);
            }else{
                return ResultUtils.failure(210,"操作失败");
            }
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 删除地址
     * */
    @PostMapping("deleteAddressById")
    public JsonResult deleteAddressById(@RequestParam("id") int id){
        try {
            int result=userService.deleteAddressById(id);
            if(result==1){
                return ResultUtils.success("删除成功",null);
            }else{
                return ResultUtils.failure(210,"删除失败");
            }
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 获取用户所有地址
     * */
    @PostMapping("getAllAddressByUuid")
    public JsonResult getAllAddressByUuid(@RequestParam("uuid") String uuid){
        try {
            return ResultUtils.success("获取成功", userService.getAllAddressByUuid(uuid));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 获取地址详情
     * */
    @PostMapping("getAddressDesc")
    public JsonResult getAddressDesc(@RequestParam("id") int id){
        try {
            return ResultUtils.success("获取成功", userService.getAddressDesc(id));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 设置默认地址
     * */
    @PostMapping("updateAddressDefault")
    public JsonResult updateAddressDefault(@RequestParam("id") int id,@RequestParam("uuid") String uuid){
        try {
            int result=userService.updateNoAddressDefault(uuid);
            if (result>0){
                userService.updateAddressDefault(id);
            }
            return  ResultUtils.success("设置成功",null);
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 获取一级分类
     * */
    @GetMapping("getOneLevelSort")
    public JsonResult getOneLevelSort(){
        try {
            return  ResultUtils.success("获取成功",userService.getOneLevelSort());
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 获取二级分类
     * */
    @GetMapping("getSecondLevelSort")
    public JsonResult getSecondLevelSort(@RequestParam("id") int id){
        try {
            return  ResultUtils.success("获取成功",userService.getSecondLevelSort(id));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 搜索
     * 符合条件的所有商品
     * */
    @PostMapping("getGoodsBySearchName")
    public JsonResult getGoodsBySearchName(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize,@RequestParam("name") String name){
        try {
            return  ResultUtils.success("获取成功",userService.getGoodsBySearchName(name,pageNum,pageSize));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 获取首页商品
     * 新品商品
     * */
    @PostMapping("getIndexNewGoods")
    public JsonResult getIndexNewGoods(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize,@RequestParam("type") int type){
        try {
            return  ResultUtils.success("获取成功",userService.getIndexNewGoods(pageNum,pageSize,type));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }
    /**
     * 获取首页商品
     * 热门推荐
     * */
    @PostMapping("getIndexPopularGoods")
    public JsonResult getIndexPopularGoods(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize){
        try {
            return  ResultUtils.success("获取成功",userService.getIndexPopularGoods(pageNum,pageSize));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 获取二级分类下商品列表
     * sort2Id传0则获取一级分类下全部商品
     * */
    @PostMapping("getGoodsBySortId")
    public JsonResult getGoodsBySortId(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize,@RequestParam("sort1Id") int sort1Id,@RequestParam("sort2Id") int sort2Id){
        try {
            return  ResultUtils.success("获取成功",userService.getGoodsBySortId(pageNum,pageSize,sort1Id,sort2Id));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 获取商品详情
     * */
    @PostMapping("getGoodsDesc")
    public JsonResult getGoodsDesc(@RequestParam("id") int id){
        try {
            return  ResultUtils.success("获取成功",userService.getGoodsDescById(id));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 获取商品是否收藏
     * */
    @PostMapping("checkGoodsIsCollect")
    public JsonResult checkGoodsIsCollect(@RequestParam("id") int id,@RequestParam("uuid") String uuid){
        try {
            return  ResultUtils.success("获取成功",userService.checkGoodsIsCollect(id,uuid));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 获取我的收藏
     * */
    @PostMapping("getMyCollectedGoods")
    public JsonResult getMyCollectedGoods(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize,@RequestParam("uuid") String uuid){
        try {
            return  ResultUtils.success("获取成功",userService.getMyCollectedGoods(pageNum,pageSize,uuid));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 取消收藏
     * */
    @PostMapping("cancelCollectionById")
    public JsonResult cancelCollectionById(@RequestParam("id") int id,@RequestParam("uuid") String uuid){
        try {
            int result=userService.cancelCollectionById(id,uuid);
            if(result>=1){
                return ResultUtils.success("操作成功",null);
            }else{
                return ResultUtils.failure(210,"操作失败");
            }
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 新增收藏
     * */
    @PostMapping("addCollectionById")
    public JsonResult addCollectionById(@RequestParam("id") int id,@RequestParam("uuid") String uuid){
        try {
            int result=userService.addCollectionById(id,uuid);
            if(result==1){
                return ResultUtils.success("操作成功",null);
            }else{
                return ResultUtils.failure(210,"操作失败");
            }
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 选择完规格跳出指定规格的商品图片 价格
     * */
    @PostMapping("getGoodsSkuByIndexStr")
    public JsonResult getGoodsSkuByIndexStr(@RequestParam("id") int id, @RequestParam("index") String index){
        try {
            return ResultUtils.success("操作成功",userService.getGoodsSkuByIndexStr(id,index));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 测试用
     * 批量新增sku
     * */
    @PostMapping("batchAddSkuTest")
    public JsonResult batchAddSkuTest(@RequestParam("id") int id){
        try {
            return ResultUtils.success("操作成功",userService.batchAddSkuTest(id));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 获取购物车列表
     * */
    @PostMapping("getMyCarGoods")
    public JsonResult getMyCarGoods(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize,@RequestParam("uuid") String uuid){
        try {
            return  ResultUtils.success("获取成功",userService.getMyCarGoods(pageNum,pageSize,uuid));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 从购物车删除商品
     * */
    @PostMapping("deleteGoodsInCarById")
    public JsonResult deleteGoodsInCarById(@RequestParam("id") int id){
        try {
            return  ResultUtils.success("获取成功",userService.deleteGoodsInCarById(id));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 加购商品到购物车
     * */
    @PostMapping("addGoodsInCar")
    public JsonResult addGoodsInCar(@RequestParam("skuId") int skuId,@RequestParam("spuId") int spuId,@RequestParam("uuid") String uuid,@RequestParam("num") int num){
        try {
            return  ResultUtils.success("获取成功",userService.addGoodsInCar(skuId,spuId,uuid,num));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 修改购物车的商品数量
     * */
    @PostMapping("updateGoodsNumInCar")
    public JsonResult updateGoodsNumInCar(@RequestParam("id") int id,@RequestParam("num") int num){
        try {
            return  ResultUtils.success("获取成功",userService.updateGoodsNumInCar(id,num));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 修改购物车的商品的规格
     * */
    @PostMapping("updateGoodsGuigeInCar")
    public JsonResult updateGoodsGuigeInCar(@RequestParam("id") int id,@RequestParam("index") String index,@RequestParam("spuId") int spuId){
        try {
            return  ResultUtils.success("获取成功",userService.updateGoodsGuigeInCar(id,index,spuId));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 根据商品的id获取此商品可选择的规格
     * */
    @PostMapping("getSpecJsonBySpuId")
    public JsonResult getSpecJsonBySpuId(@RequestParam("spuId") int spuId){
        try {
            return  ResultUtils.success("获取成功",userService.getSpecJsonBySpuId(spuId));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 提交，进入订单页
     * 购物车编号 多个逗号分割
     * */
    @PostMapping("addOrder")
    public JsonResult addOrder(@RequestParam("ids") String ids,@RequestParam("uuid") String uuid){
        try {
            return  ResultUtils.success("获取成功",userService.addOrder(ids,uuid));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 我的订单@PostMapping("getMyOrders")
     *     public JsonResult addOrder(@RequestParam("uuid") String uuid,@RequestParam("orderStatus") int orderStatus, @RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize){
     *         try {
     *             return  ResultUtils.success("获取成功",userService.getMyOrders(uuid,orderStatus,pageNum,pageSize));
     *         }catch (Exception e){
     *             return  ResultUtils.failure(501,"系统异常");
     *         }
     *     }
     * 根据订单状态筛查
     * */
    @PostMapping("getMyOrders")
    public JsonResult addOrder(@RequestParam("uuid") String uuid,@RequestParam("orderStatus") int orderStatus, @RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize){
        try {
            return  ResultUtils.success("获取成功",userService.getMyOrders(uuid,orderStatus,pageNum,pageSize));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 获取售后订单
     * */
    @PostMapping("getMyAfterOrders")
    public JsonResult getMyAfterOrders(@RequestParam("uuid") String uuid, @RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize){
        try {
            return  ResultUtils.success("获取成功",userService.getMyAfterOrders(uuid,pageNum,pageSize));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 我的订单获取详情
     * */
    @PostMapping("getMyOrderDescById")
    public JsonResult getMyOrderDescById(@RequestParam("orderId") String orderId){
        try {
            return  ResultUtils.success("获取成功",userService.getMyOrderDescById(orderId));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 我的订单-
     * 5 申请售后 4小时内可申请退款
     * 6 取消订单
     * */
    @PostMapping("updateOrderStatus")
    public JsonResult updateOrderStatus(HttpServletRequest request, @RequestParam("orderId") String orderId, @RequestParam("orderStatus") int orderStatus){
        try {
            //结果返回1 成 2超时 0修改失败
            return  ResultUtils.success("获取成功",userService.updateOrderStatus(orderId,orderStatus,request.getRequestURL().toString()));
        }catch (Exception e){
            e.printStackTrace();
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 商品页直接下单
     * */
    @PostMapping("addOrderNow")
    public JsonResult addOrderNow(@RequestParam("uuid") String uuid,@RequestParam("spuId") int spuId,@RequestParam("skuId") int skuId,@RequestParam("num") int num){
        try {
            //结果返回1 成 2超时 0修改失败
            return  ResultUtils.success("获取成功",userService.addOrderNow(uuid,spuId,skuId,num));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 意见反馈
     * */
    @PostMapping("addFeedback")
    public JsonResult addFeedback(@RequestParam("uuid") String uuid,@RequestParam("question") String question){
        try {
            //结果返回1 成 2超时 0修改失败
            return  ResultUtils.success("获取成功",userService.addFeedback(uuid,question));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

//    /**
//     * 查询物流
//     * */
//    @PostMapping("queryTrajectory2")
//    public JsonResult queryTrajectory2(@RequestParam("trackingNo") String trackingNo,@RequestParam("supplierId") int supplierId){
//        try {
//            //结果返回1 成 2超时 0修改失败
//            return  ResultUtils.success("获取成功",userService.queryTrajectory(trackingNo,supplierId));
//        }catch (Exception e){
//            return  ResultUtils.failure(501,"系统异常");
//        }
//    }

    /**
     * 查询物流
     * */
    @PostMapping("queryTrajectory")
    public JsonResult queryTrajectory(@RequestParam("trackingNo") String trackingNo){
        try {
            //结果返回1 成 2超时 0修改失败
            return  ResultUtils.success("获取成功",userService.queryTrajectory2(trackingNo));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 修改订单的地址
     * */
    @PostMapping("updateOrderAddress")
    public JsonResult updateOrderAddress(@RequestParam("orderId") String orderId,@RequestParam("addressId") int addressId,@RequestParam("type") int type){
        try {
            //结果返回1 成 2超时 0修改失败
            return  ResultUtils.success("获取成功",userService.updateOrderAddress(orderId,addressId,type));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 获取订单的状态码
     * */
    @PostMapping("checkOrderStatus")
    public JsonResult checkOrderStatus(@RequestParam("orderId") String orderId,@RequestParam("type") int type){
        try {
            //结果返回1 成 2超时 0修改失败
            return  ResultUtils.success("获取成功",userService.checkOrderStatus(orderId,type));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }


    /**
     * 修改订单为待支付状态
     * */
    @PostMapping("updateSysOrderStatus")
    public JsonResult updateSysOrderStatus(@RequestParam("orderId") String orderId,@RequestParam("type") int type){
        try {
            //结果返回1 成 2超时 0修改失败
            return  ResultUtils.success("获取成功",userService.updateSysOrderStatus(orderId,type));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 根据资源包获取分类
     * */
    @PostMapping("getTempImageSort")
    public JsonResult getTempImageSort(@RequestParam("spuId") int spuId,@RequestParam("type") int type){
        try {
            //结果返回1 成 2超时 0修改失败
            return  ResultUtils.success("获取成功",userService.getTempImageSort(spuId,type));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 根据资源包的分类编号获取图片
     * */
    @PostMapping("getTempImageBySortId")
    public JsonResult getTempImageBySortId(@RequestParam("sortId") int sortId,@RequestParam("spuId") int spuId,@RequestParam("type") int type){
        try {
            //结果返回1 成 2超时 0修改失败
            return  ResultUtils.success("获取成功",userService.getTempImageBySortId(sortId,spuId,type));
        }catch (Exception e){
            e.printStackTrace();
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 保存编辑图片之数据库
     * 直接下单，把编辑图绑定到特定订单上
     * */
    @PostMapping("saveEditImage")
    public JsonResult saveEditImage(@RequestParam("orderId") String orderId,@RequestParam("oid") int oid,@RequestParam("editImage1") String editImage1,@RequestParam("editImage2") String editImage2,@RequestParam("num") String num){
        try {
            return  ResultUtils.success("获取成功",userService.saveEditImage(orderId,oid,editImage1,editImage2,num));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 保存编辑图片之数据库
     * 直接下单，把编辑图绑定到特定订单上
     * */
    @PostMapping("saveEditImage2")
    public JsonResult saveEditImage2(@RequestParam("carId") int carId,@RequestParam("editImage1") String editImage1,@RequestParam("editImage2") String editImage2,@RequestParam("num") String num){
        try {
            return  ResultUtils.success("获取成功",userService.saveEditImage2(carId,editImage1,editImage2,num));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 获取首页营销广告
     * */
    @PostMapping("getIndexMarketingPoster")
    public JsonResult getIndexMarketingPoster(){
        try {
            return  ResultUtils.success("获取成功",userService.getIndexMarketingPoster());
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 获取订单支付状态
     * */
    @PostMapping("getSystemOrderPayStatus")
    public JsonResult getSystemOrderPayStatus(@RequestParam("orderId") String orderId){
        try {
            return  ResultUtils.success("获取成功",userService.getSystemOrderPayStatus(orderId));
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }

    /**
     * 获取产品的图片
     * imageType
     * 3.进入编辑提示图
     * 4.展示页正面
     * 5.展示页反面
     * 6.编辑页正面
     * 7.编辑页反面
     * */
    @PostMapping("getTankuangEditTipImage")
    public JsonResult getTankuangEditTipImage(@RequestParam("spuId") int spuId,@RequestParam("imageType") int imageType){
        try {
            String image=userService.getTankuangEditTipImage(spuId,imageType);
            if (image==null|| image.isEmpty()){
                return  ResultUtils.failure(101,"");
            }else{
                return ResultUtils.success("获取成功",image);
            }
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }
    @PostMapping("getEditTemplateImage")
    public JsonResult getEditTemplateImage(@RequestParam("spuId") int spuId){
        try {
            TempImage image=userService.getEditTemplateImage(spuId);
            if (image==null){
                return  ResultUtils.failure(101,"");
            }else{
                return ResultUtils.success("获取成功",image);
            }
        }catch (Exception e){
            return  ResultUtils.failure(501,"系统异常");
        }
    }
}
